package org.snowin.common;


import java.io.File;
import java.util.*;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.snowin.utils.GlobalInfo;
import org.snowin.utils.HttpSender;

public class InitWebDriver
{
    List<WebDriver> webDriverList = new ArrayList();

    public InitWebDriver() {}

    public InitWebDriver(HashMap driverHash)
    {
        Iterator iter = driverHash.entrySet().iterator();
        while (iter.hasNext())
        {
            Map.Entry entry = (Map.Entry)iter.next();
            String sp = System.getProperty("file.separator");
            String browserName = (String)entry.getKey();
            String browserLocation = (String)entry.getValue();
            if (browserName.toLowerCase().contains("ie"))
            {
                System.setProperty("webdriver.ie.driver", GlobalInfo.rootPath + sp + "bin" + sp + "tools" + sp + "IEDriverServer.exe");

                WebDriver webDriver = new InternetExplorerDriver();
                this.webDriverList.add(webDriver);
            }
            else if (browserName.toLowerCase().contains("firefox"))
            {
                if (browserLocation.equals(""))
                {
                    WebDriver webDriver = new FirefoxDriver();
                    this.webDriverList.add(webDriver);
                }
                else
                {
                    File pathToFirefoxBinary = new File(browserLocation.replace("\\", "\\\\"));
                    FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);

//                    WebDriver webDriver = new FirefoxDriver(firefoxbin, null);
                    WebDriver webDriver = new FirefoxDriver();
                    this.webDriverList.add(webDriver);
                }
            }
            else if (browserName.toLowerCase().contains("chrome"))
            {
                System.setProperty("webdriver.chrome.driver", GlobalInfo.rootPath + sp + "bin" + sp + "tools" + sp + "chromedriver.exe");

                WebDriver webDriver;
                if (browserLocation.equals("")) {
                    webDriver = new ChromeDriver();
                } else {
                    webDriver = new ChromeDriver();
                }
                this.webDriverList.add(webDriver);
            }
        }
    }

    public WebDriver initWebDrvier(String browserName, String browserLocation)
    {
        WebDriver webDriver = null;
        String sp = System.getProperty("file.separator");
        if (browserName.toLowerCase().contains("ie"))
        {
            System.setProperty("webdriver.ie.driver", GlobalInfo.rootPath + sp + "bin" + sp + "tools" + sp + "IEDriverServer.exe");

            webDriver = new InternetExplorerDriver();
        }
        else if (browserName.toLowerCase().contains("firefox"))
        {
            if (browserLocation.equals(""))
            {
                webDriver = new FirefoxDriver();
            }
            else
            {
                File pathToFirefoxBinary = new File(browserLocation.replace("\\", "\\\\"));
                FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);

//                webDriver = new FirefoxDriver(firefoxbin, null);

                webDriver = new FirefoxDriver();



            }
        }
        else if (browserName.toLowerCase().contains("chrome"))
        {
            if (!GlobalInfo.os.contains("mac")) {
                if (GlobalInfo.os.contains("win")) {
                    System.setProperty("webdriver.chrome.driver", GlobalInfo.getRootPath() + sp + "tools" + sp + "chromedriver.exe");
                } else {
                    System.setProperty("webdriver.chrome.driver", GlobalInfo.getRootPath() + sp + "tools" + sp + "chromedriver.exe");
                }
            }
            webDriver = new ChromeDriver();
        }
        else if (browserName.toLowerCase().contains("safari"))
        {
            System.setProperty("webdriver.safari.driver", browserLocation);
            webDriver = new SafariDriver();
        }
        return webDriver;
    }

    public List<WebDriver> getWebDriverList()
    {
        return this.webDriverList;
    }

    static String hostString = "http://localhost:8080";

    public String getOneProxy(String groupname)
    {
        String url = "";
        if ((null != groupname) && (groupname.trim() != "")) {
            url = hostString + "/proxyip/rest/message/getonebestproxyip/" + groupname;
        } else {
            url = hostString + "/proxyip/rest/message/getonebestproxyip/null";
        }
        String result = "";
        try
        {
            result = HttpSender.doGet4HttpClient(url, false).trim();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    public void postproxyResult(String proxyip, String groupname, String issucess)
    {
        String url = hostString + "/proxyip/rest/message/postgoodproxyip";
        String message = proxyip + ":" + groupname + "-" + issucess;
        try
        {
            String str1 = HttpSender.doPostJsonRequest(url, message, "", false);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
